package com.slaffka.services;

import com.slaffka.model.UserSession;
import com.slaffka.model.user.User;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * _User: v.tsapaev
 * Date: 22.02.12
 * Time: 0:28
 * To change this template use File | Settings | File Templates.
 */
@Service
public class UserSessionService {

    @Autowired
    private SessionFactory sessionFactory;

    @Autowired
    private ServiceFactory service;

    @Transactional
    public void createUserSession(String sessionId, User user){
        UserSession userSession = new UserSession(sessionId, user);
        sessionFactory.getCurrentSession().save(userSession);
    }
    
    @Transactional
    public void removeUserSession(String sessionId){
        UserSession userSession = getUserSessionById(sessionId);
        sessionFactory.getCurrentSession().delete(userSession);
    }

    @Transactional
    public UserSession getUserSessionById(String userSessionId){
            return (UserSession)sessionFactory.getCurrentSession()
                    .createQuery("from UserSession where userSessionId = :userSessionId")
                    .setParameter("userSessionId", userSessionId)
                    .uniqueResult();
    }

    @Transactional
    public void updateUserSession(UserSession userSession){
        sessionFactory.getCurrentSession().update(userSession);
    }

    @Transactional
    public UserSession authUser(User user, HttpSession session) {
        User userChecked = (User)service.getUserService().getUserByUsername(user.getUsername());
        UserSession userSession = getUserSessionById(session.getId());
        System.out.println("~~~authuser, userChecked = " + userChecked + ", userSession = " + userSession);
        if (userChecked != null && userChecked.getPassword().equals(user.getPassword())){
            System.out.println("~~~update userSession");
            userSession.setUser(userChecked);
            updateUserSession(userSession);
            session.setAttribute("user", userChecked);
            System.out.println("~~~updated userSession");
        }
        return userSession;
    }

    @Transactional
    public void unauthUser(HttpSession session) {
        UserSession userSession = getUserSessionById(session.getId());
        if (userSession != null){
            userSession.setUser(null);
            updateUserSession(userSession);
            session.removeAttribute("user");
        }
    }

    public List<UserSession> getUserSessions() {
        return (List<UserSession>)sessionFactory.getCurrentSession()
                .createQuery("from UserSession")
                .list();
    }
    
    public User getUserBySessionId(String sessionId){
        return (User)sessionFactory.getCurrentSession()
                .createQuery("select us.user from UserSession us where us.userSessionId = :sessionId")
                .setParameter("sessionId", sessionId)
                .uniqueResult();
    }
}
